package org.dreamfly.core.action;

import org.dreamfly.core.bean.SzModel;
import org.dreamfly.core.bean.SzModelType;
import org.dreamfly.core.page.PageResultSet;
import org.dreamfly.core.service.ModelService;
import org.dreamfly.core.service.ModelTypeService;

import java.io.IOException;
import java.util.List;

public class    ModelAction extends BaseAction {
    private PageResultSet<SzModel> resultSet;
    private SzModel model;
    private ModelService modelService;
    private ModelTypeService modelTypeService;
    private List<Long> szTypeIds;
    private List<SzModelType> types;
    private String title;
    private Long id;
    private List<Long> deleteIds;
    private String server;
    private String note;
    private String minPicUrl;
    private String isAdmin;
    private String actionName;
    private SzModel object;
    /**
     * 分页参数
     * pageSize 每页的大小
     * page     显示的页码
     */
    private Long pageSize = 5L;
    private Long page = 1L;

    /**
     * 添加模型信息
     *
     * @return success
     */
    public String addModelInfo() throws IOException {
        SzModel temp = new SzModel();
        temp.setServer(server);
        temp.setId(id);
        temp.setTitle(title);
        temp.setNote(note);
        temp.setMinPictureUrl(minPicUrl);
        modelService.addModelInfo(temp);
        getResponse().getWriter().print("success");
        return null;
    }

    /**
     * 修改模型
     *
     * @return
     */
    public String modifyModel() throws IOException {
        SzModel temp = new SzModel();
        temp.setId(id);
        temp.setTitle(title);
        temp.setNote(note);
        modelService.modifyInfo(temp);
        response.getWriter().print("success");
        return null;
    }

    /**
     * 删除模型
     *
     * @return success
     */
    public String deleteModel() {
        modelService.delete(id);
        return SUCCESS;
    }

    public String deleteModelList(){
        for(Long id : deleteIds){
            modelService.delete(id);
        }
        return SUCCESS;
    }
    public String getModelInfo(){
        object = modelService.findDetail(id);
        actionName = "modifyModel";
        return null != object ?SUCCESS :INPUT;
    }
    /**
     * 按关键字搜索模型
     *
     * @return success
     */
    /*public String sreachModel() {

        if (model == null) {
            model = new SzModel();
            model.setTitle("%");
        }
        *//*try {
            model.setTitle(new String(model.getTitle().getBytes("ISO-8859-1"), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }*//*
        if(isAdmin==null){
        	resultSet = modelService.normalSearch(model, pageSize, page, null);
        }else{
        	resultSet = modelService.normalSearch(model, pageSize, page, server);
        }
        if (resultSet.getList() == null) {
            return "error";
        }
        resultSet.setKeyWords("%");
        resultSet.setKeyWords(model.getTitle());
        return SUCCESS;
    }*/
    
    public String searchModelByType() {
        if (model == null) {
            model = new SzModel();
            model.setTitle("%");
        }
        if ("".equals(model.getTitle())){
            model.setTitle("%");
        }
        if(null == isAdmin){
        resultSet = modelService.normalSearchByType(szTypeIds, model, pageSize, page, null);
            resultSet.setActionName("modelNameSearch");
        }else {
        resultSet = modelService.normalSearchByType(szTypeIds, model, 15l, page, server);
            resultSet.setActionName("bgModel");
        }
        resultSet.setType("Model");
        if (resultSet.getList() == null) {
            return INPUT;
        }
        resultSet.setKeyWords(model.getTitle());


        return SUCCESS;
    }

    public String findType(){
        types = this.modelTypeService.findAll();
        return null != types && 0 != types.size() ? SUCCESS : INPUT;
    }

    public List<SzModelType> getTypes() {
        return types;
    }

    public void setModelTypeService(ModelTypeService modelTypeService) {
        this.modelTypeService = modelTypeService;
    }

    public String getMinPicUrl() {
        return minPicUrl;
    }

    public void setMinPicUrl(String minPicUrl) {
        this.minPicUrl = minPicUrl;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getServer() {
        return server;
    }

    public void setServer(String server) {
        this.server = server;
    }

    public String getNote() {
        return note;
    }

    public void setAdmin(String admin) {
        isAdmin = admin;
    }

    public void setDeleteIds(List<Long> deleteIds) {
        this.deleteIds = deleteIds;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public void setModelService(ModelService modelService) {
        this.modelService = modelService;
    }

    public PageResultSet<SzModel> getResultSet() {
        return resultSet;
    }

    public void setResultSet(PageResultSet<SzModel> resultSet) {
        this.resultSet = resultSet;
    }

    public Long getPageSize() {
        return pageSize;
    }

    public void setPageSize(Long pageSize) {
        this.pageSize = pageSize;
    }

    public Long getPage() {
        return page;
    }

    public void setPage(Long page) {
        this.page = page;
    }

    public SzModel getModel() {
        return model;
    }

    public void setModel(SzModel model) {
		this.model = model;
	}

	public List<Long> getSzTypeIds() {
		return szTypeIds;
	}

	public void setSzTypeIds(List<Long> szTypeIds) {
		this.szTypeIds = szTypeIds;
	}

	public String getIsAdmin() {
		return isAdmin;
	}

	public void setIsAdmin(String isAdmin) {
		this.isAdmin = isAdmin;
	}

    public String getActionName() {
        return actionName;
    }

    public SzModel getObject() {
        return object;
    }
}
